﻿<%@ page import="com.hzqy.web.vo.EpgInfoVo"%>
<%@ page import="com.hzqy.web.login.SessionKey"%>
<%@ page import="com.hzqy.commons.utils.SystemConfigUtils" %> 
<%
    String reportIp = "115.233.200.194:8082";//探针地址ip115.233.200.194:8082  localhost:8080
    String user_id = String.valueOf(session.getAttribute(SessionKey.SESSION_SERVERID));//用户账号
    EpgInfoVo epg_InfoVo = (EpgInfoVo)session.getAttribute(SessionKey.SESSION_EPGINFO_VO);
    int reportInterval = 1000*300;//探针上报频率
    String user_group_id = String.valueOf(epg_InfoVo.getGroup_path());//用户分组
    String epg_group_id = String.valueOf(epg_InfoVo.getGroup_path());//EPG分组ID,‘1’：标清版‘2’：高清版‘3’：智能版
    String stb_ip = "";//用户机顶盒IPrequest.getRemoteAddr()
    String stb_id = "";//无法获取
    String stb_type = "";//无法获取
    String stb_mac = "";//无法获取
    String terminal_type = "android_STB";//终端上报的类型：linux_STB/android_STB/Mobile/PCClient/Ipad app/iphone app/android app
    String page_id = "HQ0000";//对每一个页面都编号，用ID唯一标志，在日志服务器上维护其与url及页面名称的对应关系。
    String page_name = "环球";//页面名称，例如"首页"、“频道列表”等
    String mediacode = "";//详情页填节目编码或者电视剧编码；栏目列表页填栏目位置编码
    String medianame = "";//节目名称或者电视剧名称；栏目位所承载的栏目内容编码
    String refer_type = "3";//枚举值列表：0:未知来源；2:从频道列表页进入；2D：从频道列表详情进入；3:从栏目来源进入；3D:从栏目的详情进入；6:从推荐位进入（推荐系统专用）；7:从专题进入；8:从搜索进入；9:一般事件采集；10:从页面推荐位进入（通用）；
    String refer_page_id = "";//本项是http_refer中对应url所编号的page_id。即进入该页的前一个页面page_id；首页填空。
    String refer_page_name = "";//与refer_page_id对应的页面名称
    String definition = "0";//清   晰度0:流畅1:标清2:高清3:超清9:4k(H265)
    String bitrate = "0";//码率
    String start_time = "";//string, 取值为自从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数.如“1349348230”

    String seriescode = "";//[media code] 256 string,连续剧剧头编码mediacode,媒资的唯一编码
    String seriesflag = "0";//连续剧标识，0：节目；1:连续剧
    String refer_pos_id = "";//来源页的位置id。对页面上需要统计的位置(position)进行编号，点击它，在目标页打开后上报这个ID。没有则填空。
    String refer_pos_name = "";//来源的位置名称，位置内容编码，所在位置的栏目内容编码或节目内容编码或栏目内容编码+“||”+节目内容编码
    String refer_parent_id = "";//当前节目父级栏目ID，不存在就填空。用来追踪节目路径。
%>

<script type="text/javascript">
	var timeT1;
	var timeT2;
	var timeT3;
	var timeT4;
	var timeT5;
	var page_id = "<%=page_id%>";
    var page_name = "<%=page_name%>";
	var refer_page_id = "<%=refer_page_id%>";
	var refer_page_name = "<%=refer_page_name%>";
	var refer_pos_id = "<%=refer_pos_id%>";
    var refer_pos_name = "<%=refer_pos_name%>";
	var mediacode = "<%=mediacode%>";
	var medianame = "<%=medianame%>";
	var seriescode = "<%=seriescode%>";
	var seriesflag = "<%=seriesflag%>";
	var start_time = getStartTime();//string, 取值为自从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数.如“1349348230”

	var schedule_code = "";
	var action_type = "tv_playing";//直播tv_playing；时移timeshift
	
	var time_index =  0;
	var timeout_0 = 0;
	var xmlhttp = null;
	
	var timeout_report = 100;
	var valid_time = "";//生效起始时间
	var expired_time = "";//过期时间
	var purchase_time = "";//当为订购是，填充订购时间|格式是：YYYYMMDDHH24MISS当为退订时，填充退订时间YYYYMMDDHH24MISS
	var STBType = Authentication.CTCGetConfig("STBType");
	try{
		if(STBType.indexOf("E8205")!= -1){
			timeout_report = 200;
		}else if(STBType.indexOf("EC6106V3")!= -1){
			timeout_report = 320;
		}
	}catch(e){
		//commonTools.debug("STB = "+e);
	}
	
	function setEpgCookie(name,value){
		var cookies = name+'='+encodeURIComponent(value);
		cookies += ';max-age='+(30*60*60*24);
		cookies += ';path=/';
		document.cookie = cookies;
	}

	function getEpgCookie(key){
		var returnArgs = "";
		var arr1 = document.cookie.split('; ');
		for(var i=0;i<arr1.length;i++){
			var arr2 = arr1[i].split('=');
			if(arr2[0] == key){
				returnArgs = decodeURI(arr2[1]);
			}
		}
		if(typeof(returnArgs) == "undefined"){
			return '';
		}else{
			return returnArgs;
		}
	}
	function loadXMLDoc(_url){
		if(xmlhttp == null){
			if (window.XMLHttpRequest){
				xmlhttp=new XMLHttpRequest();
			}else{
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		xmlhttp.onreadystatechange=function(){
			//commonTools.debug("xmlhttp.readyState:"+xmlhttp.readyState+"  xmlhttp.status="+xmlhttp.status);
			if (xmlhttp.readyState==4 && xmlhttp.status==200){//
				//commonTools.debug("** success");
				loadXMLDoc_flag = 0;
			}
		};
		xmlhttp.onerror = function(){
			loadXMLDoc_flag = 0;
			//commonTools.debug("** onerror");
		};
		xmlhttp.onabort = function(){
			//commonTools.debug("** onabort");
		};
		xmlhttp.onloadstart = function(){
			//commonTools.debug("** onloadstart");
		};
		xmlhttp.onload = function(){
			//commonTools.debug("** onload");
		};
		xmlhttp.ontimeout = function(){
			loadXMLDoc_flag = 0;
			//commonTools.debug("** ontimeout");
		};
        var cookies= "cookies="+escape(document. cookie);
		xmlhttp.open("GET",_url,true);
		xmlhttp.send(cookies);
	}
	
	function CheckRequest(){
		if(xmlhttp.readyState!=4){
			timeout_0 = 0;
			time_index = 1;
			xmlhttp.abort();
			xmlhttp = null;
			clearTimeout(timeT1);
			clearTimeout(timeT2);
			clearTimeout(timeT3);
			clearTimeout(timeT4);
		}
	}
	
	function push_log(log_info) {
		//commonTools.debug("<%=reportIp%>|||"+JSON.stringify(log_info));
		var url = "http://<%=reportIp%>/?opt=put&data="+encodeURIComponent(log_info);
		try{
			//commonTools.debug("http://<%=reportIp%>/?opt=put&data="+log_info);
			loadXMLDoc(url);
		}catch(e){
			//commonTools.debug("erroe="+e);
		}
		//log_info是JSON编码格式
		return;
	}
	
	var overtime = 0;
	function overtimeTz(){
		//commonTools.debug("xmlhttp.readyState=="+xmlhttp.readyState+"||xmlhttp.status=="+xmlhttp.status);
		if(xmlhttp.readyState!=4 || xmlhttp.status!=200){
			try{
				clearTimeout(overtime);
				xmlhttp.abort();
			}catch(e){
				//commonTools.debug("停止当前请求error "+e);
			}
			//commonTools.debug("停止当前请求 overtimeTz");
		}
	}
	
	//订购日志上报
	function orderHeartbeatReport(obj){
		var log_time = getLogTime();
		var log_info = '{"action_type":"order","sys_id":"t","user_id":"<%=user_id%>","user_group_id":"<%=user_group_id%>","epg_group_id":"<%=epg_group_id%>","stb_ip":"<%=stb_ip%>","stb_id":"<%=stb_id%>","stb_type":"<%=stb_type%>","stb_mac":"<%=stb_mac%>","terminal_type":"<%=terminal_type%>","log_time":"'+log_time+'","mediacode":"'+mediacode+'","product_price":"'+obj.price+'","product_code":"'+obj.code+'","product_type":"'+obj.type+'","product_name":"'+obj.name+'","pay_type":"'+obj.pay_type+'","valid_time":"'+valid_time+'","expired_time":"'+expired_time+'","purchase_time":"'+purchase_time+'","service":"","action":"0","ordercontinue":"'+obj.ordercontinue+'"}';
		push_log(log_info);
	}

	//浏览日志上报
	function cycleHeartbeatReport() {
		try{
			if(STBType.indexOf("E900-S")!= -1&&(page_id.indexOf("HDSE00")!=-1||page_id.indexOf("HDYY0")!=-1||page_id=="HDXQ0000"||page_id.indexOf("HDYL0")!=-1||page_id.indexOf("HDJL0")!=-1)){
				timeout_report = 400;
			}
			overtime = setTimeout(overtimeTz,3000);
			clearTimeout(timeT5);
			timeT5 = setTimeout(heartbeatReport,timeout_report);
			clearTimeout(timeT1);
			timeT1 = setTimeout(cycleHeartbeatReport,<%=reportInterval%>);
		}catch(e){
			//commonTools.debug("cycleHeartbeatReport error = " +e);
		}
	}

	function heartbeatReport(){
		var log_time = getLogTime();
		var log_info = '{"action_type":"browsing","sys_id":"t","user_id":"<%=user_id%>","user_group_id":"<%=user_group_id%>","epg_group_id":"<%=epg_group_id%>","stb_ip":"<%=stb_ip%>","stb_id":"<%=stb_id%>","stb_type":"<%=stb_type%>","stb_mac":"<%=stb_mac%>","terminal_type":"<%=terminal_type%>","log_time":"'+log_time+'","page_id":"'+page_id+'","page_name":"'+page_name+'","mediacode":"'+mediacode+'","medianame":"'+medianame+'","refer_type":"<%=refer_type%>","refer_page_id":"'+refer_page_id+'","refer_page_name":"'+refer_page_name+'","refer_pos_id":"'+refer_pos_id+'","refer_pos_name":"'+refer_pos_name+'","refer_parent_id":"<%=refer_parent_id%>"}';
		push_log(log_info);
	}

	//直播，时移日志上报
	function cycleLiveHeartbeatReport() {
		liveHeartbeatReport();
		clearTimeout(timeT2);
		timeT2 = setTimeout(cycleLiveHeartbeatReport,<%=reportInterval%>);
	}

	function liveHeartbeatReport(){
		var log_time = getLogTime();
		var currentplaytime= mp.getCurrentPlayTime();
		currentplaytime= get_curtime_time(currentplaytime);
		var log_info = '{"action_type":"'+action_type+'","sys_id":"t","user_id":"<%=user_id%>","user_group_id":"<%=user_group_id%>","epg_group_id":"<%=epg_group_id%>","stb_ip":"<%=stb_ip%>","stb_id":"<%=stb_id%>","stb_type":"<%=stb_type%>","stb_mac":"<%=stb_mac%>","terminal_type":"<%=terminal_type%>","log_time":"'+log_time+'","mediacode":"'+mediacode+'","definition":"<%=definition%>","bitrate":"<%=bitrate%>","start_time":"'+start_time+'","currentplaytime":"'+currentplaytime+'","refer_type":"2","refer_page_id":"'+refer_page_id+'","refer_page_name":"'+refer_page_name+'","refer_pos_id":"<%=refer_pos_id%>","refer_pos_name":"<%=refer_pos_name%>","refer_parent_id":"<%=refer_parent_id%>"}';
		push_log(log_info);
	}

	//点播日志上报
	function cycleMediaHeartbeatReport() {
		mediaHeartbeatReport();
		clearTimeout(timeT3);
		timeT3 = setTimeout(cycleMediaHeartbeatReport,<%=reportInterval%>);
	}

	function mediaHeartbeatReport(){
		var log_time = getLogTime();
		var currentplaytime = mpPlay.getVodCurTime();
		if(currentplaytime=="-1") currentplaytime = "0";
		var log_info = '{"action_type":"vod_playing","sys_id":"t","user_id":"<%=user_id%>","user_group_id":"<%=user_group_id%>","epg_group_id":"<%=epg_group_id%>","stb_ip":"<%=stb_ip%>","stb_id":"<%=stb_id%>","stb_type":"<%=stb_type%>","stb_mac":"<%=stb_mac%>","terminal_type":"<%=terminal_type%>","log_time":"'+log_time+'","mediacode":"'+mediacode+'","seriescode":"'+seriescode+'","seriesflag":"'+seriesflag+'","definition":"<%=definition%>","bitrate":"","start_time":"'+start_time+'","currentplaytime":"'+currentplaytime+'","refer_type":"<%=refer_type%>","refer_page_id":"'+refer_page_id+'","refer_page_name":"'+refer_page_name+'","refer_pos_id":"'+refer_pos_id+'","refer_pos_name":"'+refer_pos_name+'","refer_parent_id":"<%=refer_parent_id%>"}';
		push_log(log_info);
	}

	//回看日志上报
	function backMediaHeartbeatReport() {
		backHeartbeatReport();
		clearTimeout(timeT4);
		timeT4 = setTimeout(backMediaHeartbeatReport,<%=reportInterval%>);
	}

	function backHeartbeatReport(){
		var log_time = getLogTime();
		var currentplaytime = mpPlay.getVodCurTime();
		if(currentplaytime=="-1") currentplaytime = "0";
		var log_info = '{"action_type":"tvod_playing","sys_id":"t","user_id":"<%=user_id%>","user_group_id":"<%=user_group_id%>","epg_group_id":"<%=epg_group_id%>","stb_ip":"<%=stb_ip%>","stb_id":"<%=stb_id%>","stb_type":"<%=stb_type%>","stb_mac":"<%=stb_mac%>","terminal_type":"<%=terminal_type%>","log_time":"'+log_time+'","mediacode":"'+mediacode+'","schedule_code":"'+schedule_code+'","definition":"<%=definition%>","bitrate":"","start_time":"'+start_time+'","currentplaytime":"'+currentplaytime+'","refer_type":"5","refer_page_id":"'+refer_page_id+'","refer_page_name":"'+refer_page_name+'","refer_pos_id":"<%=refer_pos_id%>","refer_pos_name":"<%=refer_pos_name%>","refer_parent_id":"<%=refer_parent_id%>"}';
		push_log(log_info);
	}


	function get_curtime_time(time_string) //20070323T142530.25Z
	{
		if(time_string=="0"){
			time_string = mp.getCurrentPlayTime();
		}
		if (time_string != "") {
			var year_val = 0, month_val = 0, day_val = 0, hour_val = 0, minute_val = 0;
			year_val = parseInt(time_string.substring(0, 4), 10);
			month_val = parseInt(time_string.substring(4, 6), 10);
			day_val = parseInt(time_string.substring(6, 8), 10);
			hour_val = parseInt(time_string.substring(9, 11), 10);
			minute_val = parseInt(time_string.substring(11, 13), 10);
			second_val = parseInt(time_string.substring(13, 15), 10);
		}
		var change_time_string = new Date(year_val, month_val - 1, day_val, hour_val+8, minute_val, second_val); //yyyy, mm, dd, hh, mm, ss;
	
		return Math.floor(change_time_string.getTime()/1000);	
	}
	
	function getLogTime(){
		var myDate = new Date();
		return Math.floor(myDate.getTime()/1000);
	}

	function getStartTime(){
		var myDate = new Date();
		return Math.floor(myDate.getTime()/1000);
	}
	
</script>